package com._520it.crm.service.impl;

import com._520it.crm.domain.Check;
import com._520it.crm.domain.OrderBill;
import com._520it.crm.domain.Organization;
import com._520it.crm.domain.Product;
import com._520it.crm.mapper.CheckMapper;
import com._520it.crm.mapper.OrganizationMapper;
import com._520it.crm.mapper.ProductMapper;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.CheckQueryObject;
import com._520it.crm.service.ICheckService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class CheckServiceImpl implements ICheckService {

    @Autowired
    private CheckMapper checkMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private OrganizationMapper organizationMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return checkMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Check record) {
        return checkMapper.insert(record);
    }

    @Override
    public Check selectByPrimaryKey(Long id) {
        return checkMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Check> selectAll() {
        return checkMapper.selectAll();
    }

    @Override
    public int updateByPrimaryKey(Check record) {
        return checkMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageResult queryPage(CheckQueryObject qo) {
        if(StringUtils.isNotBlank(qo.getProductName())){
            Product product = productMapper.selectByPrimaryKey(Long.parseLong(qo.getProductName()));
            qo.setProductName(product.getName());
        }
        if(StringUtils.isNotBlank(qo.getOrganizationNameD())){
            Organization organization = organizationMapper.selectByPrimaryKey(Long.parseLong(qo.getOrganizationNameD()));
            qo.setOrganizationNameD(organization.getOrganizationName());
        }
        if (qo.getPaymentTypeC()!=null) {
            if (qo.getPaymentTypeC()==1) {
                qo.setPaymentTypeD("刷卡缴费");
            } else {
                qo.setPaymentTypeD("支票缴费");
            }
        }
        Long count = checkMapper.queryPageCount(qo);
        if (count <= 0) {
            return new PageResult(0L, Collections.EMPTY_LIST);
        }
        List result = checkMapper.queryPageData(qo);
        return new PageResult(count,result);
    }

    @Override
    public int insertOrderBill(long orderBillId) {
        Check check = new Check();
        OrderBill orderBill = new OrderBill();
        orderBill.setId(orderBillId);
        check.setOrderBill(orderBill);
        check.setIssue(1);
        check.setPaymentType("刷卡缴费");
        check.setUnderwritingForm("核保");
        return checkMapper.insertOrderBill(check);
    }

    @Override
    public void slotCardUpdate(Check check) {
        checkMapper.slotCardUpdate(check);
    }

    @Override
    public void cheque(Check check) {
        checkMapper.cheque(check);
    }

    @Override
    public void issueSendBack(Long id) {
        checkMapper.issueSendBack(id);
    }
}
